using Publicuse.Entity;
using Publicuse.Interface;
using Publicuse.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Publicuse.Cache
{
  /// <summary>
  /// 系统常用数据缓存
  /// </summary>
  public class SysCommonCache
  {
    public static string IndexInfo { get; set; } = string.Empty;

    public static List<Role> Rolelist { get; set; } = new List<Role>();

    /// <summary>
    /// 获取系统登录页面信息
    /// </summary>
    /// <param name="mySqlSugar"></param>
    /// <param name="msg"></param>
    /// <returns></returns>
    public static string GetIndexInfo(IMySqlSugarService mySqlSugar, out string msg)
    {
      msg = string.Empty;
      try
      {
        //if (string.IsNullOrEmpty(IndexInfo))
        {
          var table = TableAtributeUtil.GetTable<Spconf>();
          var sql = $"select sValue from {table} where sCode in('loginsysremark','loginsysname');";
          var list = mySqlSugar.Query<Spconf>(sql, out msg);
          if (string.IsNullOrEmpty(msg) && list != null && list.Count > 0)
          {
            IndexInfo = string.Join("|", list.Select(o => o.sValue));
          }
        }
      }
      catch (Exception ex)
      {
        msg = ex.Message;
      }
      return IndexInfo;
    }

    /// <summary>
    /// 初始化角色信息
    /// </summary>
    /// <param name="baseServiceExt"></param>
    /// <param name="msg"></param>
    /// <returns></returns>
    public static void InitRole(IMySqlSugarService mySqlSugar)
    {
      string msg = string.Empty;
      try
      {
        Rolelist.Clear();
        var list = mySqlSugar.QueryByExpression<Role>(o => o.status != -9, out msg);
        if (string.IsNullOrEmpty(msg) && list != null && list.Count() > 0)
        {
          Rolelist = list.ToList();
        }
      }
      catch (Exception ex)
      {
        msg = ex.Message;
      }
    }
  }
}
